home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / AdobeExamples / NX_HitDetect / DetectApp.m < prev    next >
Encoding:
Text File  |  1992-12-19  |  6.8 KB  |  272 lines

  1.  
  2. /*
  3.  * (a)  (C) 1990 by Adobe Systems Incorporated. All rights reserved.
  4.  *
  5.  * (b)  If this Sample Code is distributed as part of the Display PostScript
  6.  *    System Software Development Kit from Adobe Systems Incorporated,
  7.  *    then this copy is designated as Development Software and its use is
  8.  *    subject to the terms of the License Agreement attached to such Kit.
  9.  *
  10.  * (c)  If this Sample Code is distributed independently, then the following
  11.  *    terms apply:
  12.  *
  13.  * (d)  This file may be freely copied and redistributed as long as:
  14.  *    1) Parts (a), (d), (e) and (f) continue to be included in the file,
  15.  *    2) If the file has been modified in any way, a notice of such
  16.  *      modification is conspicuously indicated.
  17.  *
  18.  * (e)  PostScript, Display PostScript, and Adobe are registered trademarks of
  19.  *    Adobe Systems Incorporated.
  20.  * 
  21.  * (f) THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO
  22.  *    CHANGE WITHOUT NOTICE, AND SHOULD NOT BE CONSTRUED
  23.  *    AS A COMMITMENT BY ADOBE SYSTEMS INCORPORATED.
  24.  *    ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY
  25.  *    OR LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO
  26.  *    WARRANTY OF ANY KIND (EXPRESS, IMPLIED OR STATUTORY)
  27.  *    WITH RESPECT TO THIS INFORMATION, AND EXPRESSLY
  28.  *    DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
  29.  *    FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT
  30.  *    OF THIRD PARTY RIGHTS.
  31.  */
  32.  
  33. /*
  34.  *    DetectApp.m
  35.  *
  36.  *    This subclass of the application class performs the global
  37.  *    setup needed for the HitDetection application. The drawing
  38.  *    window is created as well as the window buffers for redrawing
  39.  *    and moving.
  40.  *
  41.  *    Version:    2.0
  42.  *    Author:    Ken Fromm
  43.  *    History:
  44.  *            03-07-91        Added this comment.
  45.  */
  46.  
  47. #import "DetectApp.h"
  48. #import "DrawingView.h"
  49. #import "DrawingViewWraps.h"
  50. #import "DocView.h"
  51. #import "ScrollingView.h"
  52. #import <appkit/Matrix.h>
  53. #import <appkit/ScrollView.h>
  54. #import <appkit/ClipView.h>
  55. #import <appkit/Window.h>
  56. #import "HitPointView.h"
  57. #import <appkit/nextstd.h>
  58.  
  59. static NXRect            drawingRect = {0, 0, 612, 792};
  60. static NXRect            windowRect = {315, 270, 450, 500};
  61.  
  62. /*
  63. *    Create a plain window the size of the rectangle passed in and
  64. *    then insert a view into the window as a subview. A clip view
  65. *    is swapped for the content view if addclipview is YES. The
  66. *    ClipView is used for the alpha buffer, which holds the primary
  67. *    drawing. The beta buffer does not need to scroll so a ClipView
  68. *    is unnecessary.
  69. */
  70. static id createBuffer(const NXRect *winRect, BOOL  addclipview)
  71. {
  72.     id        buffer, clipview, window;
  73.  
  74.     NXRect    contRect;
  75.     
  76.     contRect.origin.x = contRect.origin.y = 0;
  77.     contRect.size = winRect->size;
  78.     window = [[Window alloc]  initContent:&contRect
  79.                 style:NX_PLAINSTYLE
  80.                 backing:NX_RETAINED
  81.                 buttonMask:0
  82.                 defer:NO] ;
  83.  
  84.     buffer = [[[[View  alloc]  initFrame:&contRect] allocateGState] setClipping:NO];
  85.     if (addclipview)
  86.     {    
  87.         clipview = [[[ClipView  alloc]  init] setFlipped:NO];
  88.         [clipview  setDisplayOnScroll:NO];
  89.         [[window  setContentView:clipview ] free];
  90.         [clipview  setDocView:buffer];
  91.     }
  92.     else
  93.         [[window contentView] addSubview:buffer];
  94.     
  95.     [window  display];
  96.  
  97.     return buffer;
  98. }
  99.  
  100. /* Resize the buffers and their windows. Called when the window resizes. */
  101. static void resizeBuffer(id buffer, const NXSize*newSize)
  102. {
  103.     NXRect        oldSize;
  104.     
  105.     [[buffer superview]  getFrame:&oldSize];
  106.     if (newSize->width > oldSize.size.width || newSize->height > oldSize.size.height)
  107.     {
  108.         [[buffer window]  sizeWindow:newSize->width :newSize->height];
  109.         [buffer sizeTo:newSize->width :newSize->height];
  110.     }
  111. }
  112.  
  113. @implementation DetectApp
  114.  
  115. + new
  116. {
  117.     self = [super new];
  118.  
  119.     hitsetting = 4.0;
  120.  
  121.     return self;
  122. }
  123.  
  124. /*
  125. *    Create the drawing window and place a scrollview as the content view.
  126. *    A DocView  instance is placed as the document view of the ClipView and then
  127. *    a DrawingView instance is placed as a subview of DocView. The DocView
  128. *    places the drawing view in the center of the window and draws the border
  129. *    and the drop shadow when the drawing view is reduced and made smaller
  130. *    than the size of the clip view. (The application is made the delegate of
  131. *    the window in order to intercept the windowDidResize message and 
  132. *    resize the buffers.)
  133. */
  134. - createWindow:(NXRect *) winRect
  135. {
  136.     id        scrollView, docView;
  137.  
  138.     NXRect    tempRect;
  139.  
  140.     windowId = [[Window alloc]  initContent:winRect
  141.             style:NX_TITLEDSTYLE
  142.             backing:NX_BUFFERED
  143.             buttonMask:NX_RESIZEBUTTONMASK
  144.             defer:NO];
  145.     [windowId  setTitle:"Hit Detection"];
  146.  
  147.     [Window  getContentRect:&tempRect  forFrameRect:winRect  style:NX_TITLEDSTYLE];
  148.     scrollView = [[ScrollingView alloc]  initFrame:&tempRect];
  149.     [scrollView setBorderType:SCROLLVIEW_BORDER];
  150.  
  151.     drawingviewId = [[DrawingView  alloc]  initFrame:&drawingRect];
  152.     docView = [[[[DocView alloc]  init]  setClipping:NO] setScale:1.0];
  153.     [scrollView  setDocView:docView];
  154.     [[docView  superview]  setFlipped:NO];
  155.     [docView  addDrawView:drawingviewId];
  156.  
  157.     [docView  placeView:drawingviewId];
  158.     [drawingviewId  createObject];
  159.  
  160.     [[windowId  setContentView:scrollView] free];
  161.  
  162.     [windowId  addToEventMask:WINDOW_MASK];
  163.     [windowId  makeFirstResponder:drawingviewId];
  164.     [windowId  setDelegate:self];
  165.  
  166.     return self;
  167. }
  168.  
  169. /* The window will free the its subviews. */
  170. - free
  171. {
  172.     [[bufferalphaId  window]  free];
  173.     [[bufferbetaId  window]  free];
  174.     [windowId  free];
  175.  
  176.     return [super free];
  177. }
  178.  
  179. - getBufferAlpha
  180. {
  181.     return bufferalphaId;
  182. }
  183.  
  184. - getBufferBeta
  185. {
  186.     return bufferbetaId;
  187. }
  188.  
  189. - getDrawingView
  190. {
  191.     return drawingviewId;
  192. }
  193.  
  194. /* 
  195. *    Places the buffers onscreen when the menu item is selected.
  196. *    Strictly for instructional purposes only.
  197. */
  198. - showBuffers:sender
  199. {
  200.     if (!showbuffers)
  201.     {
  202.         [[bufferalphaId window]  orderWindow:NX_ABOVE
  203.             relativeTo:[windowId windowNum]];
  204.         [[bufferalphaId window] moveTo:10 :10];
  205.         [[bufferbetaId window]  orderWindow:NX_ABOVE
  206.             relativeTo:[windowId windowNum]];
  207.         [[sender selectedCell] setTitle:"Hide Buffers"];
  208.         [[bufferbetaId window] moveTo:560 :10];
  209.     }
  210.     else
  211.     {
  212.         [[bufferalphaId window]  orderOut:self];
  213.         [[bufferbetaId window]  orderOut:self];
  214.         [[sender selectedCell] setTitle:"Show Buffers"];
  215.     }
  216.     showbuffers = !showbuffers;
  217.  
  218.     return self;
  219. }
  220.  
  221. - showWindow:sender
  222. {
  223.     return [windowId  makeKeyAndOrderFront:self];
  224. }
  225.  
  226. /*    Sets the hit setting to the value. */
  227. - setHitSetting:(float) value
  228. {    
  229.     hitsetting = value;
  230.  
  231.     return self;    
  232. }
  233.  
  234. /*
  235. *    Returns an unscaled the hit setting.
  236. */
  237. - (float) hitSetting
  238. {    
  239.     return hitsetting;
  240. }
  241.  
  242. /*
  243.  * Resizes the doc view and repositions the drawing view inside the doc view.
  244.  */
  245. - windowDidResize:sender
  246. {
  247.     NXSize        contSize;
  248.     
  249.     [[windowId  contentView]  getContentSize:&contSize];
  250.     resizeBuffer(bufferalphaId, &contSize);
  251.     resizeBuffer(bufferbetaId, &contSize);
  252.  
  253.     [[drawingviewId  superview]  placeView:drawingviewId];
  254.     
  255.     return self;
  256. }
  257.  
  258. - appDidInit:sender
  259. {
  260.     bufferalphaId = createBuffer(&windowRect, YES);
  261.     bufferbetaId = createBuffer(&windowRect, NO);
  262.  
  263.     [self  createWindow:&windowRect];
  264.     [windowId display];
  265.     [windowId makeKeyAndOrderFront:self];
  266.  
  267.     return self;
  268. }
  269.  
  270. @end
  271.  
  272.